package main

import (
	"errors"
	"time"

	"github.com/kotocean/cricket/auth/db"
	"github.com/kotocean/cricket/auth/token"
)

func GenerateClientMark(id int64, clientId string) string {
	token, _, err := token.GenerateToken(id, clientId, 3600)
	if err != nil {
		panic("生成token失败")
	}
	return token
}

//判断token是否过期
func ValidateToken(expiresAt int64, valid bool) (bool, error) {
	if !valid {
		return false, errors.New("token不可用")
	} else {
		if time.Now().Unix() > expiresAt {
			return false, errors.New("token过期")
		} else {
			return true, nil
		}
	}
}

func ValidatePerson(name string, password string) (bool, error) {
	person, err := db.GetPersonInfo(name)
	if err != nil {
		return false, errors.New("用户名不存在")
	}
	if person.Password != password {
		return false, errors.New("密码错误")
	}
	return true, nil
}
